﻿
namespace Common.Algorithm.Core.LocalTest;

/// <summary>
/// ParticleSwarmExecute.RunMany();
/// </summary>
public class ParticleSwarmExecute
{



    static Func<Vector<double>, double> GenFunc(out List<double[]> ranges)
    {
        HimmelblauFunction function = new HimmelblauFunction();
        ranges = function.ranges.ToList();
        Func<Vector<double>, double> func = (array) =>
        {
            return function.GenMathFunction().Invoke(array.ToArray());
        };
        return func;
    }

    /// <summary>
    ///    
    /// </summary>
    /// <param name="edges"></param>
    public static List<double[]> Run()
    {
        List<double[]> ranges = new List<double[]>();
        var func = GenFunc(out ranges);

        ParticleSwarmService particle = new ParticleSwarmService(ranges, 0.001d);
        ParticleSwarmService.denseField = func;
        Action action = () =>
        {
            particle.RunDenseAlgm(30000);
        };
        Console.WriteLine($"time:{TimeService.TimeWatch(action)}");

        var bestLoc = particle.particles.First().currentLocation;
        Console.WriteLine($"{bestLoc[0]},{bestLoc[1]}");

        return ParticleSwarmService.records;
    }

    public static void RunMany()
    {
        for (int i = 0; i < 15; i++)
        {
            Run();
        }
    }



}
